﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;

namespace DotaHelp.Helpers
{
    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public RssActionResult(IEnumerable<SyndicationItem> items)
        {            
            Feed = new SyndicationFeed("DotaHelp", "Гайды, закупы, команды, обновления", new Uri("http://dotahelp.ru")
                ,  items.OrderByDescending(_ => _.LastUpdatedTime).Take(50));
            
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";

            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }
}